﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using ArtMath.Resource;
using ArtMath.Data;
using ArtMath.Setting;
using ArtMath.Editor;
using ArtMath.Attribute;
using ArtMath.Core.Data;
using ArtMath.Core.Helper;
using ArtMath.CommonType;

namespace ArtMath.Component.CoordinateSystem
{
    /// <summary>
    /// 极轴
    /// </summary>
    [Serializable]
    public class PolarAxis : PolarCoordinateAxisBase
    {
        AngleStringX beamScale;
        public PolarAxis(SketchPad pad) : base(pad, Resources.Translate("PolarAxis"), ToolTypeName.jz, false)
        {
            RayInterval = new AngleStringX(Pad, Settings.Instance.PolarAxisBeamScale);
            //beamScale.ParametersValueChanged += (StringX sender, double oldValue) =>
            //{
            //    if (beamScale.Angle.DegreeValue < 1)
            //    {
            //        beamScale.SetExpression(Pad.AngleUnit == AngleUnit.Radian ? Angle.DToR : 1);
            //    }
            //};
        }
        [MultilingualCategory("Appearance"), Editor(typeof(AngleEditor), typeof(UITypeEditor)), MultilingualDisplayName("RayInterval")]
        public AngleStringX RayInterval
        {
            get { return beamScale; }
            set
            {
                if (beamScale is object)
                {
                    beamScale.ExpressionSet -= RayInterval_ExpressionSet;
                }
                beamScale = value;
                if (beamScale is object)
                {
                    beamScale.ExpressionSet += RayInterval_ExpressionSet;
                }
            }
        }
        void RayInterval_ExpressionSet(StringX sender, ExpressionSetEventArgs e)
        {
            if (beamScale.DegreeValue < 1)
            {
                Pad.ShowMessage(Resources.Translate("RayIntervalLess1"), MessageType.Exclamation);
                beamScale.SetExpression(e.OldExpression, false);
            }
        }
        public override void CreateComponents()
        {
            Slope = 0;
            base.CreateComponents();
        }
        protected override string getFormula()
        {
            if (Pad.CoordinateType == CoordinateSystemType.PolarCoordinate)
            {
                return "θ=0";
            }
            else
            {
                return "y=0";//可能永远无法执行
            }
        }
        public override void Paint(Graphics gra)
        {
            if (ObjectState == ObjectState.Normal && Visible)
            {
                double axisAngle = Core.Helper.Angle.StandardizeRad(linkedSC.RotateAngle.RadValue);
                PointD loc = (linkedSC.OriginCoordinate + Pad.Location) / Pad.Zoom.ToPointD();
                SizeD size = new SizeD(Pad.Canvas.ClientSize.Width / Pad.Zoom.X.Value,
                    Pad.Canvas.ClientSize.Height / Pad.Zoom.Y.Value);
                SolidBrush SB = new SolidBrush(StyleState_ == StyleState.Hover || Selected && Pad.UseReversedColor ? textColor.ReversedColor : textColor.Color);
                Pen gridPen = new Pen(StyleState_ == StyleState.Hover || Selected && Pad.UseReversedColor ? gridColor.ReversedColor : gridColor.Color, 1);
                if (ShowGrid)
                {
                    double delta = Math.Max(RayInterval.DegreeValue, 1), angle = delta;
                    while (angle < Core.Helper.Angle.DegreeRound)
                    {
                        gra.RotateTransform((float)-delta);
                        gra.DrawLine(gridPen, 0, 0, (float)Utility.CalcLength(loc, size, (axisAngle * Core.Helper.Angle.RToD + angle) % Core.Helper.Angle.DegreeRound).Y, 0);
                        angle += delta;
                    }
                    gra.RotateTransform((float)(angle - delta));
                }
                double len = Utility.CalcLength(loc, size, axisAngle * Core.Helper.Angle.RToD).Y, rulerCount = 0, r = 0;
                Pen P = new Pen(StyleState_ == StyleState.Hover || Selected && Pad.UseReversedColor ? rulerColor.ReversedColor : rulerColor.Color, 2);
                while ((rulerCount + Interval) * Scale.Value < len - 17)
                {
                    rulerCount += Interval;
                    gra.TranslateTransform((float)(Interval * Scale.Value), 0);
                    r = rulerCount * Scale.Value;
                    if (ShowGrid)
                    {
                        gra.DrawEllipse(gridPen, (float)(-r * 2), (float)-r, (float)(r * 2), (float)(r * 2));
                    }
                    if (ShowGraduation)
                    {
                        if (!rulerCount.IsInteger()) P.Width = 1;
                        gra.DrawLine(P, 0, -6, 0, 6);
                        if (!rulerCount.IsInteger()) P.Width = 2;
                    }
                    if (ShowGraduationText) gra.DrawString(FormatRuler(rulerCount),
                        new Font("Times New Roman", rulerCount.IsInteger() ? 16 : 10), SB,
                        rulerCount.IsInteger() ? new PointF(-9, GraduationTextLocation == AxisTextLocation.Below ? 6 : -29) :
                        new PointF(-5, GraduationTextLocation == AxisTextLocation.Below ? 8 : -22));
                }
                Max = rulerCount * Unit.Value;
                gra.TranslateTransform((float)-r, 0);
                gra.DrawLine(LinePen, 0, 0, (float)len, 0);//画极轴
                if (linkedSC.ShowOx)
                {
                    gra.DrawString("x", new Font("Times New Roman", 20, FontStyle.Italic), SB, (float)(len - 22), GraduationTextLocation == AxisTextLocation.Below ? -35 : 0);
                    gra.DrawString("O", new Font("Times New Roman", 20, FontStyle.Italic), SB, -30, 4);
                }
                else
                {
                    gra.DrawLine(new Pen(StyleState_ == StyleState.Hover || Selected && Pad.UseReversedColor ? rulerColor.ReversedColor : rulerColor.Color, 2), 0, -6, 0, 6);
                    gra.DrawString("0", new Font("Times New Roman", 16), SB, -9, 6);
                }
            }
            //base.Paint(gra);
        }
    }
}
